package com.cs.contract.controller;

import com.cs.contract.domain.*;
import com.cs.contract.service.*;
import com.cs.contract.util.CodeRuleHelp;
import com.cs.contract.util.CodeRuleUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author：zrx
 * @Package：com.cs.controller
 * @Project：RuoYi-Cloud
 * @name：CsContractController
 * @Date：2024/11/18 11:02
 * @Filename：CsContractController
 */
@Tag(name = "合同管理 - 招标合同")
@RestController
@RequestMapping("/contract")
public class CsContractController extends BaseController {
    private final CsContractService csContractService;
    private final CsInventoryService csInventoryService;
    private final CsPaymentService csPaymentService;
    private final CsSignService csSignService;
    private final CsCodeRulesService csCodeRulesService;

    public CsContractController(CsContractService csContractService, CsInventoryService csInventoryService, CsPaymentService csPaymentService, CsSignService csSignService, CsCodeRulesService csCodeRulesService) {
        this.csContractService = csContractService;
        this.csInventoryService = csInventoryService;
        this.csPaymentService = csPaymentService;
        this.csSignService = csSignService;
        this.csCodeRulesService = csCodeRulesService;
    }

    /**
     * 获取合同详细信息
     */
    @GetMapping("/{eid}")
    public AjaxResult getCsContractByEid(@PathVariable("eid") Integer eid) {
        CsContract csContract = csContractService.getCsContractByEid(eid);
        System.out.println("---------------------------------------------------");
        System.out.println(csContract);
        System.out.println("---------------------------------------------------");
        return success(csContract);
    }

    /**
     * 查询合同列表
     *
     * @param csContract
     * @return
     */
    @GetMapping("/listCsContract")
    public TableDataInfo listCsContract(CsContract csContract) {
        startPage();
        List<CsContract> list = csContractService.listCsContract(csContract);
        return getDataTable(list);
    }

    /**
     * 查询招标签订中合同列表
     */
    @GetMapping("/list2")
    public TableDataInfo listCsContract2(CsContract csContract) {
        System.out.println(csContract);
        startPage();
        List<CsContract> list = csContractService.listCsContract2(csContract);
        return getDataTable(list);
    }

    /**
     * 获取附件详细信息
     */
    @GetMapping("/selectCom")
    public AjaxResult getCsPubAttachmentsByEid(Integer eid) {
        return success(csContractService.getCsPubAttachmentsByEid(eid));
    }

    /**
     * 修改合同
     */
    @PutMapping
    public AjaxResult edit(@RequestBody CsContract csContract) {
        System.out.println("打印csContract--------------------------------------");
        System.out.println(csContract);
        int k = csContractService.updateCsContract(csContract);
        if (k > 0) {
            if (csContract.geteOpinion() != null) {
                return AjaxResult.success("修改成功");
            }
            Integer eid = csContract.getEid();
            // 删除设备信息
            csInventoryService.deleteCsInventoryByEid(eid);
            // 修改设备信息
            List<CsInventory> list = csContract.getCsInventoryList();
            for (CsInventory csInventory : list) {
                csInventory.setEid(eid);
                int i = csInventoryService.insertCsInventory(csInventory);
                if (i == 0) {
                    return AjaxResult.error("修改异常");
                }
            }
            // 删除支付信息
            csPaymentService.deleteCsPaymentByEid(eid);
            // 修改设备信息
            List<CsPayment> list1 = csContract.getCsPaymentList();
            for (CsPayment csPayment : list1) {
                csPayment.setEid(eid);
                int i = csPaymentService.insertCsPayment(csPayment);
                if (i == 0) {
                    return AjaxResult.error("修改异常");
                }
            }
            int i = csSignService.updateCsSign(csContract.getCsSign());
            if (i == 0) {
                return AjaxResult.error("修改异常");
            }
            if (csContract.getCsPubAttachments().getAnUrl() == null) {
                csContractService.deleteCsPubAttamentsByEid(Math.toIntExact(eid));
            } else {
                csContract.getCsPubAttachments().setEid(Math.toIntExact(eid));
                int i1 = csContractService.updateCsPubAttachments(csContract.getCsPubAttachments());
                if (i1 == 0) {
                    return AjaxResult.error("修改异常");
                }
            }
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改异常");
//        return toAjax(bsContractService.updateBsContract(bsContract));
    }

    /**
     * 合同作废
     */
    @PutMapping("/HtCancel")
    public AjaxResult HtCancel(@RequestBody CsContract csContract) {
        System.out.println(csContract);
        int i = csContractService.updateHtCancel(csContract.getEid());
        if (csContract.getGid() != null) {
            NonTenderProject pro = new NonTenderProject();
            pro.setEid(csContract.getEid());
            System.out.println("---------------");
            System.out.println(pro);
            System.out.println("---------------");
            csContractService.updateNonTenderProject(pro);
        } else {
            CsTender tender = new CsTender();
            tender.setEid(csContract.getEid());
            System.out.println("---------------");
            System.out.println(tender);
            System.out.println("---------------");
            csContractService.updateCsTender(tender);
        }
        if (i == 0) {
            return AjaxResult.success("修改失败");
        }
        return AjaxResult.success("修改成功");
    }

    /**
     * 删除合同
     */
    @GetMapping("/del")
    public AjaxResult delCsContract(Integer eid) {
        System.out.println(eid);
        csInventoryService.deleteCsInventoryByEid(eid);
        csPaymentService.deleteCsPaymentByEid(eid);
        csSignService.deleteCsSignByEid(eid);
        int i = csContractService.deleteCsContractByEid(eid);
        if (i == 0) {
            return AjaxResult.error("删除异常");
        }
        CsTender bidTender = new CsTender();
        bidTender.setEid(eid);
        csContractService.updateCsTender(bidTender);
        return AjaxResult.success("删除成功");
    }

    /**
     * 修改合同管理状态
     */
    @PutMapping("/updateoHstatus")
    public AjaxResult updateoHstatus(@RequestBody CsContract csContract) {
        return success(csContractService.updateoHstatus(csContract));
    }

    /**
     * 新增招标合同
     */
    @PostMapping
    public AjaxResult addContract(@RequestBody CsContract csContract) {
        CodeRulesResult result = CodeRuleHelp.createCode(csCodeRulesService, CodeRuleUtil.CONTROLLER);
        // 获取已经匹配的规则
        String eHcode = result.getCode();
        csContract.seteHcode(eHcode);
        String userName = SecurityUtils.getLoginUser().getSysUser().getUserName();
        csContract.setCreateBy(userName);
        csContract.seteStatus(2);
        csContract.setoHstatus(2);
        // 创建合同
        int k = csContractService.insertCsContract(csContract);
        if (k > 0) {
            Integer eid = csContract.getEid();
            CsTender csTender = new CsTender();
            csTender.setEid(eid);
            csTender.setSid(csContract.getSid());
            int i1 = csContractService.updateCsTenderEid(csTender);
            if (i1 == 0) {
                return AjaxResult.error("添加异常");
            }
            // 添加设备信息
            List<CsInventory> list1 = csContract.getCsInventoryList();
            if (list1 != null) {
                for (CsInventory csInventory : list1) {
                    csInventory.setEid(eid);
                    int i = csInventoryService.insertCsInventory(csInventory);
                    if (i == 0) {
                        return AjaxResult.error("添加异常");
                    }
                }
            }
            List<CsPayment> list2 = csContract.getCsPaymentList();
            if (list2 != null) {
                for (CsPayment csPayment : list2) {
                    csPayment.setEid(eid);
                    int i = csPaymentService.insertCsPayment(csPayment);
                    if (i == 0) {
                        return AjaxResult.error("添加异常");
                    }
                }
            }
            System.out.println("合同信息：" + csContract);
            CsSign csSign = csContract.getCsSign();
            csSign.setEid(eid);
            int i = csSignService.insertCsSign(csSign);
            if (i == 0) {
                return AjaxResult.error("添加异常");
            }
            CsPubAttachments csPubAttachments = csContract.getCsPubAttachments();
            if (csPubAttachments.getAnUrl() != null) {
                csPubAttachments.setEid(Math.toIntExact(eid));
                int k1 = csContractService.insertCsPubAttachments(csPubAttachments);
                if (k1 == 0) {
                    return AjaxResult.error("添加异常");
                }
            }
            return AjaxResult.success("添加成功");
        }
        return AjaxResult.error("添加异常");
    }
}
